const canvas = document.getElementById('dynamicCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const particlesArray = [];
const starsArray = [];

// 粒子类 - 移除生命周期，保持永久存在
class Particle {
    constructor() {
        this.x = Math.random() * canvas.width;
        this.y = Math.random() * canvas.height;
        this.size = Math.random() * 3 + 1;
        this.speedX = (Math.random() - 0.5) * 2;
        this.speedY = (Math.random() - 0.5) * 2;
        this.color = `rgba(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, 0.8)`;
    }
    
    update() {
        this.x += this.speedX;
        this.y += this.speedY;
        
        // 边界检查 - 保持粒子在画布内
        if (this.x < 0) this.x = canvas.width;
        if (this.x > canvas.width) this.x = 0;
        if (this.y < 0) this.y = canvas.height;
        if (this.y > canvas.height) this.y = 0;
    }
    
    draw() {
        ctx.fillStyle = this.color;
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
        ctx.closePath();
        ctx.fill();
        
        // 绘制粒子间连线
        for (let i = 0; i < particlesArray.length; i++) {
            const other = particlesArray[i];
            const dx = this.x - other.x;
            const dy = this.y - other.y;
            const distance = Math.sqrt(dx * dx + dy * dy);
            
            if (distance > 20 && distance < 100 && other !== this) {
                // 根据距离计算连线透明度
                const alpha = Math.min(0.3, (120 - distance) / 400);
                ctx.beginPath();
                ctx.strokeStyle = `rgba(255, 255, 255, ${alpha})`;
                ctx.lineWidth = 0.8;
                ctx.moveTo(this.x, this.y);
                ctx.lineTo(other.x, other.y);
                ctx.stroke();
                ctx.closePath();
            }
        }
    }
}

// 星星类保持不变
class Star {
    constructor() {
        this.x = Math.random() * canvas.width;
        this.y = Math.random() * canvas.height;
        this.size = Math.random() * 2;
        this.speedX = (Math.random() - 0.5) * 0.5;
        this.speedY = (Math.random() - 0.5) * 0.5;
        this.twinkle = Math.random() * 0.5 + 0.5;
    }
    
    update() {
        this.x += this.speedX;
        this.y += this.speedY;
        
        if (this.x < 0) this.x = canvas.width;
        if (this.x > canvas.width) this.x = 0;
        if (this.y < 0) this.y = canvas.height;
        if (this.y > canvas.height) this.y = 0;
        
        this.twinkle += (Math.random() - 0.5) * 0.05;
        this.twinkle = Math.max(0.5, Math.min(1, this.twinkle));
    }
    
    draw() {
        ctx.fillStyle = `rgba(255, 255, 255, ${this.twinkle})`;
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
        ctx.closePath();
        ctx.fill();
    }
}

// 初始化函数
function init() {
    particlesArray.length = 0;
    starsArray.length = 0;
    
    // 根据屏幕尺寸动态调整粒子数量
    const particleCount = Math.floor(canvas.width * canvas.height / 2000);
    const starCount = Math.floor(canvas.width * canvas.height / 1000);
    
    for (let i = 0; i < particleCount; i++) {
        particlesArray.push(new Particle());
    }
    
    for (let i = 0; i < starCount; i++) {
        starsArray.push(new Star());
    }
}

// 处理粒子 - 移除生命周期检查
function handleParticles() {
    for (let i = 0; i < particlesArray.length; i++) {
        particlesArray[i].update();
        particlesArray[i].draw();
    }
}

// 处理星星保持不变
function handleStars() {
    for (let i = 0; i < starsArray.length; i++) {
        starsArray[i].update();
        starsArray[i].draw();
    }
}

// 动画循环保持不变
function animate() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    handleParticles();
    handleStars();
    requestAnimationFrame(animate);
}

// 响应窗口大小变化保持不变
window.addEventListener('resize', function() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    init();
});

// 初始化并启动动画
init();
animate();